package at.hockey.ticker;

import at.hockey.ticker.R;
import android.content.Context;
import android.graphics.Color;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.View.OnTouchListener;

public class MatchDetailsEntryUI extends LinearLayout implements OnTouchListener {

	private GestureDetector mGestureDetector;
	private HockeyTickerUI mHTUI;
	private TextView mTV1, mTV2, mTV3, mTV4;
	
	public MatchDetailsEntryUI(HockeyTickerUI root, MatchDetails md, MatchDetailsEntry mde) {
		super(root);

		this.mHTUI = root;
		
		//instantiate xml layout
		LayoutInflater inflater = (LayoutInflater) this.mHTUI.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.matchdetailsentry, this);
		
        this.mGestureDetector = new GestureDetector(new OnFlingGestureListener(this.mHTUI));
		
		this.mTV1 = (TextView) this.findViewById(R.id.mde_team_button);
		this.mTV1.setText(mde.getTeam());
		this.mTV1.setOnTouchListener(this);

		this.mTV2 = (TextView) this.findViewById(R.id.mde_type_button);
		this.mTV2.setText(mde.getType());
		this.mTV2.setOnTouchListener(this);

		if(mde.getTeam().equals(md.getTeam1())){
			this.mTV1.setTextColor(Color.parseColor("#FF3636"));
			this.mTV2.setTextColor(Color.parseColor("#FF3636"));
		}
		else{
			this.mTV1.setTextColor(Color.parseColor("#367CFF"));
			this.mTV2.setTextColor(Color.parseColor("#367CFF"));
		}
		
		this.mTV3 = (TextView) this.findViewById(R.id.mde_text1_view);
		this.mTV3.setText(mde.getText1());

		this.mTV4 = (TextView) this.findViewById(R.id.mde_text2_view);
		this.mTV4.setText(mde.getText2());
	}

	@Override
	public boolean onTouch(View v, MotionEvent e) {

		if(mGestureDetector.onTouchEvent(e)){
			return true;
		}
		else{
			float eventX = v.getLeft() + e.getX();
			float eventY = v.getTop() + e.getY();
			
			if(e.getAction() == MotionEvent.ACTION_UP && eventX >= v.getLeft() && eventX <= v.getRight() && eventY >= v.getTop() && eventY <= v.getBottom()){

				if(v.getId() == R.id.mde_team_button){
					this.mTV4.setText("Team Button Pressed");
				}
				else if(v.getId() == R.id.mde_type_button){
					this.mTV4.setText("Type Button Pressed");
				}
			}
			return true;
		}
	}

}
